package com.example.wasaimovie.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wasaimovie.Mapper.CartMapper;
import com.example.wasaimovie.Model.entity.Arrangement;
import com.example.wasaimovie.Model.entity.Cart;
import com.example.wasaimovie.Model.entity.Film;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.ArrangementService;
import com.example.wasaimovie.Service.CartService;
import com.example.wasaimovie.Service.FilmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    CartService cartService;

    @Autowired
    ArrangementService arrangementService;

    @Autowired
    FilmService filmService;

    @Value("${movie.image.url.path}")
    String url;

    @Override
    public List<Map<String, Object>> getMsgByCart(String uid) {
        List<Map<String, Object>> result = new ArrayList<>();
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        List<Cart> list = cartService.list(qw);
        for (Cart cart : list) {
            // 获得排片信息
            QueryWrapper<Arrangement> qw1 = new QueryWrapper<>();
            qw1.eq("id", cart.getAid());
            Arrangement arrangement = arrangementService.list(qw1).get(0);

            // 获得电影信息
            String fid = arrangement.getFid();
            QueryWrapper<Film> filmQueryWrapper = new QueryWrapper<Film>();
            filmQueryWrapper.eq("id", fid);
            Film film = filmService.list(filmQueryWrapper).get(0);

            // 整合结果
            Map<String, Object> map = new HashMap<>();
            film.setCover(url + film.getCover());
            map.put("film", film);
            map.put("arrangement", arrangement);
            map.put("cart", cart);
            result.add(map);
        }

        return result;
    }

    @Override
    public ResponseResult<Object> deleteCartById(String id) {
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq("id", id);
        boolean remove = cartService.remove(qw);
        if (remove){
            return new ResponseResult<>(200, "");
        }
        return new ResponseResult<>(400, "请求失败");
    }
}
